home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / lpr / startdaemon.c < prev   
C/C++ Source or Header  |  1990-02-16  |  3KB  |  130 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #ifndef lint
  14. static char sccsid[] = "@(#)startdaemon.c    5.2 (Berkeley) 5/5/88";
  15. #endif /* not lint */
  16.  
  17. /*
  18.  * Tell the printer daemon that there are new files in the spool directory.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <sys/file.h>
  25. #include "lp.local.h"
  26.  
  27. static perr();
  28.  
  29. startdaemon(printer)
  30.     char *printer;
  31. {
  32. #ifndef sprite
  33.         struct sockaddr_un sun;
  34. #endif
  35.     register int s, n;
  36.     char buf[BUFSIZ];
  37.  
  38. #ifdef sprite
  39.     gethostname(buf, sizeof(buf));
  40.     sprintf(pdevName, "/hosts/%s/dev/printer", buf);
  41.     s = open (pdevName, O_WRONLY, 0);
  42.     if (s < 0) {
  43.         perror(pdevName);
  44.         /*
  45.          * the daemon may be dead, so we attempt to start up a new one
  46.          */
  47.          switch (fork()) {
  48.  
  49.          case 0:
  50.             fprintf(stderr, "attempting to restart lpd\n");
  51.             execl("/sprite/daemons.$MACHINE/lpd", "lpd", 0);
  52.         perror("exec failed");
  53.         return 0;
  54.  
  55.          case -1:
  56.             perror("cannot fork");
  57.         return 0;
  58.  
  59.          default:
  60.             /* loop while we wait for the daemon to start up */
  61.             for (n = 60; --n >= 0;) {
  62.             sleep(1);
  63.             if ((s = open (pdevName, O_WRONLY, 0)) >= 0)
  64.             break;
  65.         }
  66.         if (s < 0)
  67.             return 0;
  68.         fprintf(stderr, "lpd restarted\n");
  69.         break;
  70.          }
  71.     }
  72.  
  73.     (void) sprintf(buf, "\1%s\n", printer);
  74.     n = strlen(buf);
  75.     if (write(s, buf, n) != n) {
  76.         perr("write");
  77.         (void) close(s);
  78.         return(0);
  79.     }
  80.     (void) close(s);
  81.     return(1);
  82. #else
  83.     s = socket(AF_UNIX, SOCK_STREAM, 0);
  84.     if (s < 0) {
  85.         perr("socket");
  86.         return(0);
  87.     }
  88.     sun.sun_family = AF_UNIX;
  89.     strcpy(sun.sun_path, SOCKETNAME);
  90.     if (connect(s, &sun, strlen(sun.sun_path) + 2) < 0) {
  91.         perr("connect");
  92.         (void) close(s);
  93.         return(0);
  94.     }
  95.     (void) sprintf(buf, "\1%s\n", printer);
  96.     n = strlen(buf);
  97.     if (write(s, buf, n) != n) {
  98.         perr("write");
  99.         (void) close(s);
  100.         return(0);
  101.     }
  102.     if (read(s, buf, 1) == 1) {
  103.         if (buf[0] == '\0') {        /* everything is OK */
  104.             (void) close(s);
  105.             return(1);
  106.         }
  107.         putchar(buf[0]);
  108.     }
  109.     while ((n = read(s, buf, sizeof(buf))) > 0)
  110.         fwrite(buf, 1, n, stdout);
  111.     (void) close(s);
  112.     return(0);
  113. #endif
  114. }
  115.  
  116. static
  117. perr(msg)
  118.     char *msg;
  119. {
  120.     extern char *name;
  121.     extern int sys_nerr;
  122.     extern char *sys_errlist[];
  123.     extern int errno;
  124.  
  125.     printf("%s: %s: ", name, msg);
  126.     fputs(errno < sys_nerr ? sys_errlist[errno] : "Unknown error" , stdout);
  127.     putchar('\n');
  128. }
  129.  
  130.